<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>

</head>
<body>
<div id="app1">
    <el-page-header @back="goBack" content="用户管理页面">
    </el-page-header>
</div>
<div id="app">

    <!--搜索表单-->
    <el-form :inline="true" :model="user2" class="demo-form-inline">

        <el-form-item label="姓名">
            <el-input v-model="user2.name" placeholder="姓名"></el-input>
        </el-form-item>

        <el-form-item label="性别">
            <el-input v-model="user2.sex" placeholder="性别"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>

    <!--按钮-->

    <el-row>

        <el-button type="danger" plain @click="deleteByids">批量删除</el-button>
        <el-button type="primary" plain @click="dialogVisible = true">新增</el-button>

    </el-row>
    <!--添加数据对话框表单-->
    <el-dialog
            title="新增用户"
            :visible.sync="dialogVisible"
            width="30%"
            >
        <el-form ref="form" :model="user1" label-width="80px">
        <el-form-item label="用户名">
            <el-input v-model="user1.usersname"></el-input>
        </el-form-item>

        <el-form-item label="密码">
            <el-input v-model="user1.password"></el-input>
        </el-form-item>

        <el-form-item label="姓名">
            <el-input v-model="user1.name"></el-input>
        </el-form-item>

        <el-form-item label="工作">
            <el-input v-model="user1.work"></el-input>
        </el-form-item>
        <el-form-item label="兴趣">
            <el-input v-model="user1.interest"></el-input>
        </el-form-item>
        <el-form-item label="性别">
            <el-input v-model="user1.sex"></el-input>
        </el-form-item>

        <el-form-item label="个人说明">
            <el-input type="textarea" v-model="user1.remark"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="addBrand">提交</el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
        </el-form>

    </el-dialog>


    <el-dialog
            title="修改用户"
            :visible.sync="dialogVisible1"
            width="30%"
    >

        <el-form ref="form" :model="user1" label-width="80px">
            <el-form-item label="用户名">
                <el-input v-model="user1.usersname" disabled="disabled"></el-input>
            </el-form-item>

            <el-form-item label="密码">
                <el-input v-model="user1.password"></el-input>
            </el-form-item>

            <el-form-item label="姓名">
                <el-input v-model="user1.name"></el-input>
            </el-form-item>

            <el-form-item label="工作">
                <el-input v-model="user1.work"></el-input>
            </el-form-item>
            <el-form-item label="兴趣">
                <el-input v-model="user1.interest"></el-input>
            </el-form-item>
            <el-form-item label="性别">
                <el-input v-model="user1.sex"></el-input>
            </el-form-item>

            <el-form-item label="个人说明">
                <el-input type="textarea" v-model="user1.remark"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="update">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>

    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"
        >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>

            <el-table-column
                    prop="usersname"
                    label="用户名"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="password"
                    label="密码"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="name"
                    align="center"
                    label="名字">
            </el-table-column>
            <el-table-column
                    prop="work"
                    align="center"
                    label="工作">
            </el-table-column>
            <el-table-column
                    prop="interest"
                    align="center"
                    label="兴趣">
            </el-table-column>
            <el-table-column
                    prop="sex"
                    align="center"
                    label="性别">
            </el-table-column>
            <el-table-column
                    prop="remark"
                    align="center"
                    label="个人说明">
            </el-table-column>


            <el-table-column
                    fixed="right"
                    label="操作"
                    width="100">
                <template slot-scope="scope">
                    <el-button plain @click="bianji(scope.row)" type="text" size="small">修改</el-button>
                    <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
                </template>
            </el-table-column>

        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>

</div>


<script src="../asset/js/vue.js"></script>
<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="../asset/js/axios-0.18.0.js"></script>
<script>
    new Vue({
        el: "#app",
        mounted(){

            this.selectAll();
        },
        methods: {
            selectAll(){
                // var  _this=this;
                // axios({
                //     method:"get",
                //     url:"http://localhost:8080/01_46_luowei_war/user/selectAll"
                // }).then(function (resp){
                //     _this.tableData= resp.data;
                // })
                var  _this=this;
                axios({
                    method:"post",
                    url:"http://localhost:8080/01_46_luowei_war/user/selectByPageAndCondition?currentPage="+_this.currentPage+"&pageSize="+_this.pageSize+"",
                    data:_this.user2
                }).then(function (resp){
                    _this.tableData= resp.data.rows;
                    _this.totalCount=resp.data.totalCount;
                })
            },

            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            // 复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(this.multipleSelection)
            },
            // 查询方法
            onSubmit() {
                this.selectAll();
            },
            // 添加数据

            addBrand(){
                var _this=this;

                axios({
                    method: "post",
                    url: "http://localhost:8080/01_46_luowei_war/user/addUser",
                    data:_this.user1
                }).then(function (resp){
                    if (resp.data==="success"){
                        _this.dialogVisible = false;
                        _this.selectAll();

                        _this.$message({
                            message: '恭喜你，添加成功',
                            type: 'success'
                        });
                    }
                    if (resp.data==="shibai"){
                        _this.dialogVisible = false;
                        _this.selectAll();
                        _this.$message({
                            message: '账号重复，添加失败',
                            type: 'success'
                        });
                    }
                })
            },
            bianji(row){
                var _this=this;
                _this.dialogVisible1 = true;
                this.user1=row;
            },
            del(row){
                var _this=this;

                axios({
                    method: "post",
                    url: "http://localhost:8080/01_46_luowei_war/user/deleteByid",
                    data: row.id
                }).then(function (resp){
                    if (resp.data==="success"){
                        _this.selectAll();
                        _this.$message({
                            message: '恭喜你，删除成功',
                            type: 'success'
                        });
                    }
                })
            },
            update(){
                var _this=this;

                axios({
                    method: "post",
                    url: "http://localhost:8080/01_46_luowei_war/user/updateplus",
                    data:_this.user1
                }).then(function (resp){
                    if (resp.data==="success"){
                        _this.dialogVisible1 = false;
                        _this.selectAll();
                        _this.$message({
                            message: '恭喜你，修改成功',
                            type: 'success'
                        });
                    }
                })
            },
            //分页
            handleSizeChange(val) {
                //console.log(`每页 ${val} 条`);
                this.pageSize=val;
                this.selectAll()
            },
            handleCurrentChange(val) {
                //console.log(`当前页: ${val}`);
                this.currentPage=val;
                this.selectAll()
            },
            deleteByids(){//批量删除
                var _this=this;
                for (let i=0;i<this.multipleSelection.length;i++){
                    let selection= this.multipleSelection[i];
                    this.selectedIds[i]=selection.id;
                }
                axios({
                    method: "post",
                    url: "http://localhost:8080/01_46_luowei_war/user/deleteByids",
                    data: _this.selectedIds
                }).then(function (resp){
                    if (resp.data==="success"){
                        _this.selectAll();
                        _this.$message({
                            message: '恭喜你，删除成功',
                            type: 'success'
                        });
                    }
                })
            }
        },
        data() {
            return {
                tableData:[],
                //总记录数
                totalCount: 100,
                // 当前页码
                currentPage: 1,
                pageSize: 5,
                // 添加数据对话框是否展示的标记
                dialogVisible: false,
                dialogVisible1: false,
                // 品牌模型数据
                user: {
                    usersname: '',
                    password: '',
                    name: '',
                    work:"",
                    interest:"",
                    sex:"",
                    remark:""
                },
                user1: {
                    usersname: '',
                    password: '',
                    name: '',
                    work:"",
                    interest:"",
                    sex:"",
                    remark:""
                },
                user2: {
                    usersname: '',
                    password: '',
                    name: '',
                    work:"",
                    interest:"",
                    sex:"",
                    remark:""
                },
                selectedIds:[],
                // 复选框选中数据集合
                multipleSelection: [],

            }
        }
    })
    var Main = {
        methods: {
            goBack() {
                console.log('go back');
                window.location.href="../index.jsp";
            }
        }
    }
    var Ctor = Vue.extend(Main)
    new Ctor().$mount('#app1')
</script>

</body>
</html>